#encoding: UTF-8
require "rubygems"
require "date"
require "twitter"
require "mysql2"
require "rtopsy"


class Twiteando
  # To change this template use File | Settings | File Templates.

end
search = Twitter::Search.new

#i=1
#
#Twitter.user_timeline("LigaBBVA",{count:200}).each do |twetts|
#  puts i
#  puts twetts.text
#  i+=1
#end

#Twitter.trends_daily(Date.parse("2011-10-01")).each do |result|
#
#  puts result.to_s
#end


# connecting to database
conect =  Mysql2::Client.new(:host => 'localhost',:username => 'root',:password => 'root',:database => 'resfut')

tweets = []
words = []
#resultado = []

result = Topsy::API.search('from%3ALigaBBVA+Final.+%23lfp', :a,1,100)
puts result.to_s
result.list.each do |var|
  puts var.content
end

=begin
search.from("LigaBBVA").hashtag("LigaBBVA").containing("Final. ").each do |r|


  tweets = r.text.to_s.split("\n")
  #puts tweets
  tweets.each do |tweet|
    words = tweet.split(" ")
    #resultado = words[3].split("-")
    puts tweet
    cuadro1 = /([a-zA-Z]*)\s(\d)/.match(tweet)
    #cuadro2 = /-[\s]?\d\s([\S]*[\s]?[\S]?)[\s|\(]?/.match(tweet.gsub('á','a'))
    cuadro2 = /-[\s]?(\d)\s([\S]*[\s|\w]*)[\s|\(]?/.match(tweet.gsub('á','a'))
    resultado = //.match(tweet)
    #puts cuadro2.captures[0]
    conect.query("INSERT INTO `resultados`(`liga`, `cuadro1`, `cuadro2`, `resultado`) VALUES ('#{words[0]}','#{cuadro1.captures[0]}','#{cuadro2.captures[1]}','#{cuadro1.captures[1]}-#{cuadro2.captures[0]}');")

  end
  #puts "#{r.from_user}: #{r.text.to_s}"
end
while search.next_page? do
  search.fetch_next_page.each do |r|
  puts "#{r.from_user}: #{r.text.to_s} #{r.date.to_s}"
  end
end
=end
#
#search = Twitter::Search.new.containing("Final.").since_date("2011-09-05").until_date("2011-10-11")
#
##get all the tweets
#tweets = search.fetch
#next_tweets = search.fetch_next_page
#while(next_tweets != nil)
#  tweets = tweets + next_tweets
#  next_tweets = search.fetch_next_page
#end
#
#return tweets.first(@max_tweets)

